package com.musemun.util;

import com.alibaba.excel.EasyExcel;

import com.alibaba.fastjson.JSON;
import com.musemun.common.ResultCode;
import com.musemun.config.Listener.ExcelListener;
import com.musemun.exception.ApiException;
import com.musemun.pojo.Crifo;
import com.musemun.pojo.ExcelCrifo;
import com.musemun.service.impl.CrifoServiceImpl;
import com.sun.scenario.effect.ImageData;
import org.apache.commons.logging.LogFactory;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.ReadListener;
import com.musemun.config.Listener.ExcelListener;
import com.musemun.config.Listener.OutBatchListener;
import com.musemun.pojo.Crifo;
import com.musemun.pojo.OutExcel;
import com.musemun.service.function.LendContact;
import com.musemun.service.function.functionimpl.LendContactImpl;
import com.musemun.service.impl.CrifoServiceImpl;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
//import org.junit.Test;
import org.junit.platform.commons.logging.Logger;
import org.junit.platform.commons.logging.LoggerFactory;
import org.springframework.util.ResourceUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URLEncoder;
import java.util.*;
import javax.annotation.Resource;
import java.io.InputStream;

/**
 * @author winner
 * @description:
 * @date 2020/4/13
 */
public class ExcelUtil<T> {
    Logger log= LoggerFactory.getLogger(ExcelUtil.class);
    /**
     * @param inputStream excel的输入流
     * @return void
     */
    public static void ExcelUpload(InputStream inputStream) {
        EasyExcel.read(inputStream, Crifo.class, new ExcelListener(new CrifoServiceImpl())).sheet().doRead();
    }

    /**
     *
     *
     * @param list  需要导出的数据
     * @param response  resoponse
     * @return void
     */
    public static <T> void ExcelDownload(List<T> list, HttpServletResponse response) throws IOException {
        List<ExcelCrifo> dataList = new LinkedList<>();
//        crifo->ExcelCrifo
        list.forEach(data -> {
            if (data instanceof Crifo) {
                try {
                    dataList.add(new ExcelCrifo((Crifo) data));
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
            }
        });
        System.out.println("list==="+list);
        System.out.println("datalist=="+dataList);
        try {
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("文物表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            // 这里需要设置不关闭流
            if(dataList.isEmpty())
            {
                EasyExcel.write(response.getOutputStream(), list.get(0).getClass()).autoCloseStream(Boolean.FALSE).sheet("文物数据表")
                        .doWrite(list);
            } else
                EasyExcel.write(response.getOutputStream(), dataList.get(0).getClass()).autoCloseStream(Boolean.FALSE).sheet("文物数据表")
                        .doWrite(dataList);


        } catch (Exception e) {
            // 重置response
            response.reset();
            throw new ApiException(ResultCode.EXCEL_NOT_DATA);
//            // 重置response
//            response.reset();
//            response.setContentType("application/json");
//            response.setCharacterEncoding("utf-8");
//            Map<String, String> map = new HashMap<String, String>();
//            map.put("status", "failure");
//            map.put("message", "数据库没有数据" + e.getMessage());
//            response.getWriter().println(JSON.toJSONString(map));
        }
    }

    public static <T> void ExcelDownload(List<T> list) {

    }

    public static void main(String[] args) {

    }

    /**
     * 出库批量出库
     * @param inputStream
     */
    @Resource
    public static void lendModelInsertBatch(InputStream inputStream){
        System.out.println("--------------------------------------------------------------------------------------------------------------------------------------------------------------");
        EasyExcel.read(inputStream, OutExcel.class, new OutBatchListener()).sheet().doRead();

    }

}
